package y2017.date0311pm.学生排序;

/**
 * 班级类
 */
public class Class {
    private Student[] students;

    public Student[] getStudents() {
        return students;
    }

    public void setStudents(Student[] students) {
        this.students = students;
    }

    /**
     * 根据学生成绩进行排序，成绩高在后面
     *
     * @param students数组
     * @return 根据学生成绩排序后的students数组
     */
    public Student[] sort(Student[] stus) {
        //副本。用于返回；保证传进来的数组没有发生变化。实现了序列化接口则可以直接使用clone()方法
        Student[] students = new Student[stus.length];
        for (int i = 0; i < stus.length; i++) {
            students[i] = stus[i];
        }

        //对副本数组进行排序
        for (int curr = 0; curr < students.length - 1; curr++) {

            for (int next = curr + 1; next < students.length; next++) {
                //比较当前成绩和下一个成绩
                int currSorce = students[curr].getSorce();
                int nextSorce = students[next].getSorce();
                if (currSorce > nextSorce) {//如果大就交换位置
                    Student tempStu = students[next];
                    students[next] = students[curr];
                    students[curr] = tempStu;
                }

            }
        }
        return students;

    }
}
